<template>
  <div class="mod-config">
      <el-tabs v-model="activeName" @tab-click="handleClick" type="border-card">
          <el-tab-pane label="结款管理" name="first">
            <el-form :inline="true" :model="dataForm" @keyup.enter.native="searchHandle">
              <el-row>
                <el-form-item label="销售类型">
                  <el-select v-model="dataForm.saleType">
                    <el-option v-for="item in saleTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
                  </el-select>
                  <!-- <el-input v-model="dataForm.id" placeholder="请输入商品类别ID"></el-input> -->
                </el-form-item>
                <el-form-item label="销售单号">
                  <el-input v-model="dataForm.billId"></el-input>
                </el-form-item>
                <el-form-item label="顾客姓名">
                  <el-input v-model="dataForm.customerName"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
                  <el-button icon="el-icon-delete" @click="clearFormData">清空</el-button>
                </el-form-item>
              </el-row>
            </el-form>

            <div class="avue-crud">
              <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading">
                <el-table-column label="操作" width="150" fixed="right">
                  <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" v-if="scope.row.saleStatus == '17'" @click="paymentCancelHandle(scope.row)">撤销结款</el-button>
                    <el-button type="text" icon="el-icon-edit" v-if="scope.row.saleStatus == '1'" @click="paymentHandle(scope.row)">结款</el-button>
                    <el-button v-if="permissions.eims_sale_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <el-table-column prop="billId" label="销售单号" width="160">
                  <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="checkDetailByOrder(scope.row.billId)">{{scope.row.billId}}</el-button>
                  </template>
                </el-table-column>
                <el-table-column prop="customerName" label="顾客姓名" width="100"></el-table-column>
                <el-table-column prop="salerUserName" label="销售人员" width="100"></el-table-column>
                <el-table-column prop="saleType" label="销售类型" width="100">
                  <template slot-scope="scope">
                    {{getNameByValue(scope.row.saleType,saleTypeData,'saleTypeData')}}
                  </template>
                </el-table-column>
                <el-table-column prop="originalPrice" label="原价合计"></el-table-column>
                <el-table-column prop="discountPrice" label="折扣金额"></el-table-column>
                <el-table-column prop="removePrice" label="抹零金额"></el-table-column>
                <el-table-column prop="receivablePrice" label="应收金额"></el-table-column>
                <el-table-column prop="paidPrice" label="实缴金额"></el-table-column>
                <el-table-column label="销售状态" width="100">
                  <template slot-scope="scope">
                    {{getNameByValue(scope.row.saleStatus,saleStatusOption,'saleStatusOption')}}
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <div class="avue-crud__pagination">
              <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                :total="totalPage"
                background
                layout="total, sizes, prev, pager, next">
              </el-pagination>
            </div>
          </el-tab-pane>
          <el-tab-pane label="打印配镜单" name="second">
            <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList">
              <el-row>
                <el-form-item label="销售类型">
                  <el-select v-model="dataForm.saleType">
                    <el-option v-for="item in saleTypeData" :value="item.value" :label="item.label" :key="item.value"></el-option>
                  </el-select>
                  <!-- <el-input v-model="dataForm.id" placeholder="请输入商品类别ID"></el-input> -->
                </el-form-item>
                <el-form-item label="销售单号">
                  <el-input v-model="dataForm.billId"></el-input>
                </el-form-item>
                <el-form-item label="顾客姓名">
                  <el-input v-model="dataForm.name"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button icon="el-icon-search" type="primary" @click="getDataList">查询</el-button>
                  <el-button icon="el-icon-delete" @click="clearFormData">清空</el-button>
                </el-form-item>
              </el-row>
            </el-form>

            <div class="avue-crud">
              <el-table :data="dataList" border v-loading="dataListLoading">
                <el-table-column label="操作" width="220">
                  <template slot-scope="scope">
                    <a :href="printUrl" target="_blank"><el-button type="text" icon="el-icon-printer" style="margin-right: 10px;" @click="saleGlassPrint(scope.row.id)">打印配镜单</el-button></a>
                    <a :href="printUrl" target="_blank"><el-button type="text" icon="el-icon-printer" @click="refundFormPrint(scope.row.id)">打印退款单</el-button></a>
                  </template>
                </el-table-column>
                <el-table-column prop="billId" label="销售单号">
                  <template slot-scope="scope">
                    <el-button type="text" @click="checkDetailByOrder(scope.row.billId)">{{scope.row.billId}}</el-button>
                  </template>
                </el-table-column>
                <el-table-column prop="name" label="顾客姓名"></el-table-column>
                <el-table-column prop="createUserName" label="销售人员"></el-table-column>
                <el-table-column prop="originalPrice" label="原价合计"></el-table-column>
                <el-table-column prop="receivablePrice" label="应收金额"></el-table-column>
                <el-table-column prop="paidPrice" label="实缴金额"></el-table-column>
              </el-table>
            </div>
            <div class="avue-crud__pagination">
              <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                :total="totalPage"
                background
                layout="total, sizes, prev, pager, next">
              </el-pagination>
            </div>
          </el-tab-pane>
        </el-tabs>
      <!-- 弹窗，  结账 -->
      <payment v-if="paymentVisible" ref='payment' :saleDataList="dataList" @refreshDataList="getDataList"></payment>
      <!-- 弹窗，配镜单详情 -->
      <order-info v-if="infoVisible" ref="infoDialog"></order-info>
      <!--  -->
      <cancel-pay v-if="cancelVisible" ref="cancelPay" @refreshDataList="getDataList"></cancel-pay>
  </div>
</template>

<script>
  import { remote } from '@/api/admin/dict'
  import { getCtgList } from '@/api/eims/ctg'
  import {fetchMatchingList,delMatchingObj} from '@/api/eims/cashier/payment'
  // import { fetchReturnMatchingPage } from '@/api/eims/cashier/refund'
  import {getSaleList} from '@/api/eims/sale'
  import { getSaleStatus } from "@/api/eims/optician/glassesinfo";
  import Payment from './Payment'
  import OrderInfo from '@/views/eims/optician/glassesinfo/detail'
  import CancelPay from './CancelPay'

  import {mapGetters} from 'vuex'
  export default {
    data () {
      return {
        activeName: 'first',
        saleStatusOption: [],
        printUrl: '',
        dataForm: {
          saleType: '',
          billId: '',
          customerName: ''
        },
        ctgOption: '',
        dataList: [],
        saleTypeData: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        paymentVisible: false,
        infoVisible: false,
        cancelVisible: false
      }
    },
    created () {
      this.getOpticianStatus()
      this.getDataList()
      remote('sale_type').then(result => {
        this.saleTypeData = result.data.data
      })
    },
    computed: {
      ...mapGetters(['permissions','userInfo','bizData'])
    },
    components:{
      Payment,
      OrderInfo,
      CancelPay
    },
    methods: {
      handleClick(tab, event) {
        // this.activeName = tab.name
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
        this.getDataList()
      },
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
      clearFormData() {
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.dataForm.deptId = this.userInfo.lessDeptId
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = this.filterParams(this.dataForm)
        if(this.activeName == 'first'){
          fetchMatchingList(Object.assign(params1,params2)).then(response => {
            if(response.data.code == '0'){
              this.dataList = response.data.data.records
              this.totalPage = response.data.data.total
              this.dataListLoading = false
            }else{
              this.$message.error(response.data.msg)
            }
          })
        }else{
          getSaleList(Object.assign(params1,params2)).then(response => {
            if(response.data.code == '0'){
              this.dataList = response.data.data.records
              this.totalPage = response.data.data.total
              this.dataListLoading = false
            }else{
              this.$message.error(response.data.msg)
            }
          })
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 结账
      paymentHandle(data) {
        this.paymentVisible = true
        this.$nextTick(() => {
          this.$refs.payment.init(data)
        })
      },
      // 撤销结款
      paymentCancelHandle(data){
        this.cancelVisible = true
        this.$nextTick(() => {
          this.$refs.cancelPay.init(data)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
          return delMatchingObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  查看配镜单详情
      checkDetailByOrder(order) {
        this.infoVisible = true;
        this.$nextTick(() => {
          this.$refs.infoDialog.init('', order, '配镜单详情');
        });
      },
      //  根据value值获取name
      getNameByValue(value,option,name) {
        let obj = {}
        if(name == 'saleTypeData'){
          obj = option.find(item => {
            return item.value == value
          })
          if(obj) {
            return obj.label
          }
        }else if(name == 'saleStatusOption'){
          obj = option.find(item => {
            return item.id == value
          })
          if(obj) {
            return obj.name
          }
        }
      },
      //  获取商品类别列表
      getGoodsCtgList() {
        getCtgList().then(result => {
          this.ctgOption = result.data.data
        })
      },
      //  打印配镜单
      saleGlassPrint(id) {
          let params =''
          if(this.userInfo.lessDeptId=='2')
          {
              params = '/saleGlassPrint_xprinter.cpt&op=view&wFlag=1&saleId='
          }else {
              params = '/saleGlassPrint_ykyy.cpt&wFlag=1&saleId='
          }
        this.printUrl = this.bizData.printUrl + params + id
      },
      //  打印退款单
      refundFormPrint(id){
          let params =''
          if(this.userInfo.lessDeptId=='2')
          {
              params = '/saleGlassPrint_xprinter.cpt&op=view&wFlag=-1&saleId='
          }else
          {
              params = '/saleGlassPrint_ykyy.cpt&wFlag=-1&saleId='
          }
        this.printUrl = this.bizData.printUrl + params + id
      },
      // 获取在途状态列表
      getOpticianStatus() {
        getSaleStatus().then(response => {
          if(response.data.code == '0'){
            this.saleStatusOption = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
    }
  }
</script>
